define(function(require, exports){
	var $		= require("jquery");
	var pubjs	= require('pubjs');
	var view	= require('@base/view');
	var util	= require('util');
	var tip    = require('@base/tip');
	var common = require('modules/common');

	// 旧修改预算
	var Charge = view.widget.extend({
		init: function( config ) {
			config = pubjs.conf( config, {
				"infoUrl": 'campaign/info',
				"setBudgetUrl": 'campaign/setBudget',
				"layout": {
					"module": '@base/dialog.base',
					"mask": 1,
					"title": LANG('修改预算'),
					"showFoot": true, // 显示底层的取消和完成按钮
					"width": 400,
					"close": null,
					"silence": false // 点击按钮发消息
				},
				"view_model": {
					"budget": '',
					"totalBudget": '',
					"placeholder": '0.00'
				}
			});
			this.Super( 'init', arguments );
			//缓存id
			this.$id = 0;
		},
		afterBuild:function(){
			var self = this;
			var el = self.getDOM();
			var html = this.$con = $([
				'<div class="P-campaignCharge ">',
					'<div class="M-formConGroup">',
						'<label class="group-label">' + LANG('总预算：') + '</label>',
						'<div class="group-content">',
							'<input type="text" class="mr10 ml10 M-formInput" ms-duplex="totalBudget" ms-attr-placeholder="placeholder"/>',
							'<big>' + LANG('元') + '</big>',
						'</div>',
					'</div>',
					'<div class="M-formConGroup">',
						'<label class="group-label">' + LANG('每日预算：') + '</label>',
						'<div class="group-content">',
							'<input type="text" class="mr10 ml10 M-formInput" ms-duplex="budget" ms-attr-placeholder="placeholder"/>',
							'<big>' + LANG('元') + '</big>',
						'</div>',
					'</div>',
				'</div>'
			].join(''));
			html.appendTo(el);
		},
		load: function(id) {
			var self = this;
			self.$id = id;
			this.showLoading();
			pubjs.mc.send( this.getConfig('infoUrl'), {
				'_id': +id
			}, self.onData.bind(this));
		},
		onData: function(err, data) {
			this.hideLoading();
			if(err){
				pubjs.alert(err.message);
				return false;
			}
			this.setData(data);
		},
		setData: function (data){
			if(!data){
				return false;
			}
			var vm = this.vm.$;
			vm.totalBudget = data.TotalBudget || '';
			vm.budget = data.Budget || '';
		},
		getData: function(data){
			var vm = this.vm.$;
			return {
				'_id': this.$id,
				'TotalBudget': +vm.totalBudget || 0,
				'Budget': +vm.budget || 0
			}
		},
		reset: function(){
			this.vm.reset();
			return this;
		},
		save: function(){
			var data = this.getData();

			if(this.validate(data)){
				this.showLoading();
				var c = this.getConfig();
				pubjs.mc.send(c.setBudgetUrl, data, this.onSave.bind(this));
			}
		},
		onSave: function(err, data){
			this.hideLoading();
			if(err){
				pubjs.alert(err.message);
				return false;
			}
			pubjs.alert(LANG('修改预算成功'));
			this.hide();
			this.parent().reload();
			return false;
		},
		//响应点击完成事件
		onDialogOk: function(ev){
			this.save();
			return false;
		},
		validate: function(data){
			if(!util.isNumber(data.Budget) || !util.isNumber(data.TotalBudget)){
				pubjs.alert(LANG('输入金额非法'));
				return false;
			}
			return true;
		}
	});
	exports.base = Charge;

	// 新修改预算
	var CampaignBudget = tip.base.extend({
		init: function(config){
			config = pubjs.conf(config, {
				'width':220,
				'autoHide': 'click_body',
				'view_model': {
					"budget": '',
					"totalBudget": '',
					"placeholder": '0.00'
				}
			});

			//缓存id
			this.$id = 0;

			this.Super('init', arguments);
		},
		afterBuild: function(){
			this.Super('afterBuild', arguments);
			var self = this;
			// var el = self.getDOM();
			var doms = self.$doms;
			var html = this.$con = $([
				'<div class="P-campaignCharge ">',
					'<div class="M-formConGroup">',
						'<label class="group-label">' + LANG('总预算：') + '</label>',
						'<div class="group-content">',
							'<input type="text" class="mr10 ml10 M-formInput w100" ms-duplex="totalBudget" ms-attr-placeholder="placeholder"/>',
							'<big>' + LANG('元') + '</big>',
						'</div>',
					'</div>',
					'<div class="M-formConGroup">',
						'<label class="group-label">' + LANG('每日预算：') + '</label>',
						'<div class="group-content">',
							'<input type="text" class="mr10 ml10 M-formInput w100" ms-duplex="budget" ms-attr-placeholder="placeholder"/>',
							'<big>' + LANG('元') + '</big>',
						'</div>',
					'</div>',
					'<div class="M-formButtons">',
						'<span pub-name="cancel" pub-mod="@base/common/input.button" ></span>',
						'<span pub-name="save" pub-mod="@base/common/input.button" ></span>',
					'</div>',
				'</div>'
			].join(''));
			html.appendTo(doms.content);

			self.buildFromTemplate({
				'save': {
					'value': LANG('保存'),
					'class': 'btn btn-confirm',
					'width': 80
				},
				'cancel': {
					'value': LANG('取消'),
					'class': 'btn btn-link mr20',
					'width': 80
				}
			});
		},
		setData: function(data){
			if(data){
				var vm = this.vm.$;
				vm.totalBudget = data.TotalBudget || '';
				vm.budget = data.Budget || '';
				this.$id = data._id || 0;
			}
			return this;
		},
		getData: function(){
			var vm = this.vm.$;
			return {
				'_id': this.$id,
				'TotalBudget': vm.totalBudget || 0,
				'Budget': vm.budget || 0
			}
		},
		onInputClick: function(ev){
			switch (ev.name){
				case 'cancel':
					this.cancel();
					break;
				case 'save':
					this.save();
					break;
			}
			return false;
		},
		cancel: function(){
			this.hide();
			return this;
		},
		save: function(){
			var self = this;
			var data = self.getData();
			if(!util.isNumber(data.Budget) || !util.isNumber(data.TotalBudget)){
				pubjs.alert(LANG('输入金额非法'));
				return false;
			}
			data.Budget = +data.Budget;
			data.TotalBudget = +data.TotalBudget;

			pubjs.mc.send('campaign/setBudget', data, self.onSave.bind(self));
		},
		onSave: function(err, data){
			if(err){
				pubjs.alert(err.message);
				return false;
			}
			pubjs.alert(LANG('修改预算成功'));
			if (common.checkOpener() && window.opener && window.opener.pubjs)
			{
				window.opener.pubjs.core.cast('saveCampaignSuccess', {"id": this.$id});
			}
			this.fire('saveCampaignSuccess', {'id': this.$id});
			this.vm.reset();
			this.hide();
			return false;
		},
		reset: function(){
			this.$id = 0;
			this.vm.reset();
			return this;
		}
	});
	exports.campaignBudget = CampaignBudget;
});